package com.xiaohao.communication.daoImpl;

import com.xiaohao.communication.bean.ConcernAndUnsubscribe;
import com.xiaohao.communication.dao.UserConcernAndUnsubscribeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;


/*
* 用户关注与退订
* */
@Repository
public class UserConcernAndUnsubscribeDaoImpl implements UserConcernAndUnsubscribeDao{


    @Autowired
    MongoTemplate mongoTemplate;

    //关注用户
    @Override
    public void concernUser(ConcernAndUnsubscribe concernAndUnsubscribe) {
        //设置被关注用户状态码为 1
        concernAndUnsubscribe.setWithUsernameState(1);
        mongoTemplate.save(concernAndUnsubscribe);
    }


    /*
     * 取消关注用户
     * withusername:被关注用户名
     * Username:关注用户名
     * 传入之后，修改  被关注用户名状态码  ，置为0
     * */
    @Override
    public void cancelConcernUser(String withUsername, String username) {

//        这个and（）  要比andOperator（）方法 好用，因为一个Criteria中只能有一个 andOperator（），and（）可以多个
        Query query = new Query(Criteria.where("withUsername").is(withUsername).and("username").is(username));

        Update update = new Update();
        update.set("withUsernameState",0);

        mongoTemplate.updateFirst(query,update,ConcernAndUnsubscribe.class);


    }


    /*
     * 获取关注用户
     * username：关注用户名
     * 传入 关注用户名，
     * */
    @Override
    public List<ConcernAndUnsubscribe> getConcernUser(String username) {
        List<ConcernAndUnsubscribe> username1 = mongoTemplate.find(new Query(Criteria.where("username").is(username)), ConcernAndUnsubscribe.class);
        return username1;

    }



    /*******************************/

    //关注话题
    @Override
    public void concernTopic(ConcernAndUnsubscribe concernAndUnsubscribe) {

        String username = concernAndUnsubscribe.getUsername();
        int topic_id = concernAndUnsubscribe.getTopic_id();

        Query query = new Query(Criteria.where("username").is(username));
        Update update = new Update();
        update.set("topic_id",topic_id);
        update.set("topicState",1);//一定为 1，状态码

        mongoTemplate.updateFirst(query,update,ConcernAndUnsubscribe.class,"concernAndUnsubscribe");

    }

    //取消关注话题
    /*
    * topic_id  ： 关注话题id
    * username  ： 关注用户
    * 传入参数 设置被关注话题状态码 为 0；
    * */
    @Override
    public void cancelConcernTopic(int topic_id, String username) {

        Query query = new Query(Criteria.where("topic_id").is(topic_id).and("username").is(username));

        Update update = new Update();
        update.set("topicState",0);

        mongoTemplate.updateFirst(query,update,ConcernAndUnsubscribe.class,"concernAndUnsubscribe");


    }


    //获取关注话题
    @Override
    public List<ConcernAndUnsubscribe> getConcernTopic(String username) {

        List<ConcernAndUnsubscribe> username1 = mongoTemplate.find(new Query(Criteria.where("username").is(username)), ConcernAndUnsubscribe.class);

        return username1;
    }



}
